﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace CommonLib.System.FileSystem
{
    public class FileUtil
    {

        #region constants & class
        private const int SW_SHOW = 5;
        private const uint SEE_MASK_INVOKEIDLIST = 12;
        [DllImport("shell32.dll")]
        static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);

        [StructLayout(LayoutKind.Sequential)]
        public struct SHELLEXECUTEINFO
        {
            public int cbSize;
            public uint fMask;
            public IntPtr hwnd;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpVerb;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpFile;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpParameters;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpDirectory;
            public int nShow;
            public IntPtr hInstApp;
            public IntPtr lpIDList;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpClass;
            public IntPtr hkeyClass;
            public uint dwHotKey;
            public IntPtr hIcon;
            public IntPtr hProcess;
        }
        #endregion

        /// <summary>
        /// 以Bytes,KB,MB,GB的单位获取文件大小
        /// </summary>
        /// <param name="length">文件长度（Byte为单位）</param>
        /// <returns></returns>
        public static string GetFileSize(long length)
        {
            string result = null;
            if (length < 1024)
                result = String.Format("{0} Bytes", length);
            else if (length < Math.Pow(2, 20))
                result = String.Format("{0:N2} KB", length / 1024.0);
            else if (length < Math.Pow(2, 30))
                result = String.Format("{0:N2} MB", length / Math.Pow(2, 20));
            else if (length < Math.Pow(2, 40))
                result = String.Format("{0:N2} GB", length / Math.Pow(2, 30));
            return result;
        }

        /// <summary>
        /// 获取文件类型
        /// </summary>
        /// <param name="extension">文件扩展名</param>
        /// <returns></returns>
        public static string GetFileType(string extension)
        {
            RegistryKey rKey = null, sKey = null;
            string fileType = null;
            try
            {
                rKey = Registry.ClassesRoot;
                sKey = rKey.OpenSubKey(extension);
                if (sKey != null && (string)sKey.GetValue("", extension) != extension)
                {
                    sKey = rKey.OpenSubKey((string)sKey.GetValue("", extension));
                    fileType = (string)sKey.GetValue("");
                }
                else
                    fileType = extension.Substring(extension.LastIndexOf('.') + 1).ToUpper() + " 文件";
                return fileType ?? "";
            }
            finally
            {
                if (sKey != null) sKey.Close();
                if (rKey != null) rKey.Close();
            }
        }

        /// <summary>
        /// 获取所选目录下的所有文件
        /// </summary>
        /// <param name="directories"></param>
        /// <returns></returns>
        public static List<string> GetFiles(string directory)
        {
            List<string> directories = new List<string>();
            directories.Add(directory);
            return GetFiles(directories);
        }

        /// <summary>
        /// 获取所选目录下的所有文件
        /// </summary>
        /// <param name="directories"></param>
        /// <returns></returns>
        public static List<string> GetFiles(List<string> directories)
        {
            List<string> result = new List<string>();
            List<string> subDirectories = new List<string>();//子目录
            foreach (string directory in directories)
            {
                result.AddRange(Directory.GetFiles(directory));
                subDirectories.AddRange(Directory.GetDirectories(directory));
            }
            if (subDirectories.Count == 0)
                return result;
            else
            {
                result.AddRange(GetFiles(subDirectories));//递归读取所有子目录下的文件
                return result;
            }
        }

        public static void ShowProperties(string fileName)
        {
            SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
            info.cbSize = Marshal.SizeOf(info);
            info.lpVerb = "properties";
            info.lpFile = fileName;
            info.nShow = SW_SHOW;
            info.fMask = SEE_MASK_INVOKEIDLIST;
            ShellExecuteEx(ref info);
        }
    }
}
